<template>
  <div class="search">
    <div class="searc-box-wrapper">
      <searchbox ref="searchbox"  @querry = "onQuerryChange"></searchbox>
    </div>
    <div class="shortcut-wrapper" v-show="!querry" ref="shortcutwrapper">
      <scroll :refreshDelay = "refreshDelay" class="shortcut" :data = "shortcut" ref="shortcut">
        <div>
          <div class="hotkey">
            <h1 class="title">热门搜索</h1>
            <ul>
              <li class="item" v-for="(item,index) in hotkeys" :key = "index" @click="setquerry(item.k)">
                <span>{{item.k}}</span>
              </li>        
            </ul>
          </div>
          <div class="search-history" v-show="searchHistory.length">
            <h1 class="title">
              <span class="text">搜索历史</span>
              <span>
                <i class="icon-delete-normal" @click="showconfirm"></i>
              </span>
            </h1>
            <searchlist 
            :searches = "searchHistory" 
            @delete = "deleteOne"
            @select = "setquerry"></searchlist>
          </div>
        </div>
      </scroll>
    </div>
    <div class="search-result" v-show="querry" ref="searchResult">
      <suggest :querry = "querry" ref="suggest" @listScroll = "listScroll" @search = "saveSearch"></suggest>
    </div>
    <confirm @confirm = "clearhistory" ref="confirm" text = "是否确定删除全部历史记录"></confirm>
    <router-view></router-view>
  </div>
</template>

<script>
import searchbox from 'base/searchbox/searchbox.vue'
import {gethHotKeys} from "api/search.js"
import {ERR_OK} from "api/config"
import Suggest from 'components/suggest/suggest.vue'
import {playListMixin,searchMixin} from "commom/js/mixin.js"
import searchlist from "base/searchlist/searchlist.vue"
import confirm from "base/confirm/confirm.vue"
import scroll from "base/scroll/scroll.vue"
import {mapActions} from 'vuex'
export default {
  mixins:[playListMixin,searchMixin],
  data(){
    return{
      hotkeys:[],
      
    }
  },
  created(){
    this._getHotKeys()
  },
  computed:{
    shortcut(){
      return this.hotkeys.concat(this.searchHistory)
    },
  },
  methods:{
    handlePlayList(playList){
      const bottom = playList.length > 0 ? '60px' : '';
      this.$refs.searchResult.style.bottom = bottom
      this.$refs.suggest.refresh()

      this.$refs.shortcutwrapper.style.bottom = bottom
      this.$refs.shortcut.refresh()

    },

    _getHotKeys(){
      gethHotKeys().then((res)=>{
        if(res.code === ERR_OK){
          this.hotkeys = res.data.hotkey.slice(0,10)
        }
      })
    },

    showconfirm(){
      this.$refs.confirm.show()
    },
    clearhistory(){
      this.clearSearchHistory();
    },
    ...mapActions([
      'clearSearchHistory'
    ])
  },
  watch:{
    querry(newquerry){
      if(!newquerry){
        setTimeout(()=>{
          this.$refs.shortcut.refresh()
        },20)
      }
    }
  },
  components: { 
    searchbox,
    Suggest,
    searchlist,
    confirm,
    scroll
  },

}
</script>

<style lang="stylus" scoped>
@import "~commom/stulys/variable.styl"
.search
  .searc-box-wrapper
    margin 20px
  .search-result
    position fixed
    top 178px
    width 100%
    bottom 0
  .shortcut-wrapper
    position fixed
    top 178px
    width 100%
    bottom 0px
    .shortcut
      overflow hidden
      height 100%
      .search-history
        margin 0 20px 20px 20px
        .title
          margin-bottom: 20px
          font-size: $font-size-medium
          color: $color-text-l   
          display flex
          align-items center
          justify-content space-between        
      .hotkey
        margin 0 20px 20px 20px
        .title
          margin-bottom: 20px
          font-size: $font-size-medium
          color: $color-text-l 
        .item
          display inline-block
          padding 5px 10px
          margin 0 20px 10px 0
          border-radius 6px
          background-color $color-highlight-background
          font-size: $font-size-medium
          color $color-text-d
</style>
